﻿using SBlogCore.Common.JWTExtend.Helpers;
using SBlogCore.Common.JWTExtend.Models;

namespace SBlogCore.Application
{
    /// <summary>
    /// 
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class BaseController: ControllerBase
    {
        #region Token处理
        /// <summary>
        /// 获取用户Token对象
        /// </summary>
        /// <returns>Claims对象</returns>
        protected TokenModelJwt GetTokenUser()
        {
            var tokenModel = JwtHelper.SerializeJwt(GetToken());
            return tokenModel;
        }
        /// <summary>
        /// 获取Token
        /// </summary>
        /// <returns>Token字符串</returns>
        protected string GetToken()
        {
            var tokenHeader = Request.Headers["Authorization"].ToString().Replace("Bearer ", "");
            return tokenHeader;
        } 
        #endregion

        #region 返回结果包装
        /// <summary>
        /// 处理成功
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="msg"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        protected ResultEntity<T> Success<T>(string msg, T obj)
        {
            return ResultEntity<T>.Success(msg, obj);
        }
        /// <summary>
        /// 处理成功
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        protected ResultEntity<T> Success<T>(T obj)
        {
            return Success("操作成功！", obj);
        }
        /// <summary>
        /// 处理失败
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="msg"></param>
        /// <returns></returns>
        protected ResultEntity<T> Error<T>(string msg)
        {
            return ResultEntity<T>.Error(msg);
        }
        /// <summary>
        /// 处理失败
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        protected ResultEntity<T> Error<T>()
        {
            return Error<T>("操作失败");
        } 
        #endregion
    }
}
